<script setup lang="ts">
import { onLaunch, onPageNotFound, onShow } from "@dcloudio/uni-app";
import { user_info_api } from "@/api/guider_home";
import { AppData } from "./utils/app";
import { Account } from "./utils/account";

onLaunch((e) => {
  console.log("App Launch", e);

  // 窗口属性、状态栏、底部导航条
  const systemInfo = uni.getSystemInfoSync();
  const windowWidth = systemInfo.windowWidth;
  const statusBarHeight = systemInfo.statusBarHeight || 0;
  const menuButtonRect = uni.getMenuButtonBoundingClientRect();
  const menuButtonWidth = menuButtonRect.width;
  const menuButtonHeight = menuButtonRect.height;
  const menuButtonTop = menuButtonRect.top;
  const navBarHeight = menuButtonHeight + (menuButtonTop - statusBarHeight) * 2;
  AppData.rectData = {
    windowWidth, // 可视窗口宽度
    statusBarHeight, // 状态栏高度
    menuButtonWidth, // 胶囊按钮宽度
    menuButtonHeight, // 胶囊按钮高度
    menuButtonTop, // 胶囊按钮上边距
    // 胶囊按钮右边距，屏幕宽度 - 胶囊按钮距离导航栏右边距 - 胶囊按钮宽度
    menuButtonRight: windowWidth - menuButtonRect.left - menuButtonWidth,
    navBarHeight, // 导航栏高度，胶囊按钮高度 + 胶囊按钮距离状态栏高度 * 2
    // 顶部高度，状态栏高度 + 导航栏高度
    topBarHeight: statusBarHeight + navBarHeight,
  };
  console.log("AppData.rectData", AppData.rectData);
  // 系统类型
  AppData.systemType = systemInfo.system.toLowerCase().includes("ios")
    ? "ios"
    : "android";
  console.log("AppData.systemType", AppData.systemType);
  // 初始化 domain
  AppData.domain =
    AppData.env === "prod"
      ? "https://ss.sanmaoyou.com"
      : "https://sstest.sanmaoyou.com";
  console.log("AppData.domain", AppData.domain);
  // 获取app配置
  AppData.bundleData = {
    bundleName: "smytbgg",
    versionName: "1.0.0",
    tagId: "113",
  };
  console.log("AppData.bundleData", AppData.bundleData);
});
onShow(() => {
  // 刷新用户数据
  if (Account.isLogin) {
    user_info_api().then((resp) => {
      const userData = AppData.userData;
      userData.third_img = resp.avatar_url;
      userData.nick_name = resp.nickname;
      userData.is_guide_certification = resp.is_guide_certification;
      userData.status = resp.status;
      userData.is_vip_member = resp.is_vip_member;
      AppData.userData = userData;
    });
  }
});
onPageNotFound((e) => {
  console.log("onPageNotFound", e);
});
</script>

<style lang="scss">
page {
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
  background-color: #f9f9f9;
  box-sizing: border-box;

  // 重置 navigator 组件 hover 样式
  .navigator-hover {
    background-color: transparent;
    opacity: 1;
  }
}
</style>
